function onLogout() {
  $.ajax({
    url: '/logout',
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function onCheck(){
  var snum = document.getElementById('getsnum').value;
  hit_load(snum);
  document.getElementById('bingo').style.top = '80px';
}

function table_load() {
  $.ajax({
    url: '/realtime',
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        init_all_table();
        updateTable(data);
        $('.td1').on("click",function(){
          console.log($(this).text());
          /*console.log($(this).offset());*/
          hit_load($(this).text());
          document.getElementById('bingo').style.top = $(this).offset().top+'px';
        })
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

function hit_load(sname){
  $.ajax({
    url: '/hit?snum='+sname,
    type: 'get',
    success: function(data,status){
      if(status == 'success'){
        console.log(data);
        lines = data.split('\n');
        var str = '';
        for(var i in lines){
          items = lines[i].split(',')
          str+='<tr>'
          for(var j in items){
            if(j>0){
              str+='<td>'+items[j]+'</td>'
            }
          }
          str+='</tr>'
        }
        $('#hit-table').html('<tr><td>日期</td><td>模型</td><td>收益1</td><td>收益2</td><td>收益3</td><td>收益4</td><td>收益5</td></tr>');
        $('#hit-table').append(str);
        $('#hit-name').html(sname);
        document.getElementById('bingo').style.display = 'block';
      }
    },
    error: function(data,err){
      console.log(data);
    }
  });
}

window.onload = init();
function init(){
  document.getElementById('bingo').style.display = 'none';
  document.getElementById('bingo').style.position = 'absolute';
  document.getElementById('bingo').style.right = '10px'
  document.getElementById('bingo').style.top = '80px'
  document.getElementById('bingo').style.width = '500px'
  document.getElementById('bingo').style.backgroundColor = 'black'
  document.getElementById('bingo').style.opacity = '0.8'
  document.getElementById('bingo').style.color = 'green'
  document.getElementById('bingo').style.paddingBottom = '20px'
  init_all_table();
}

function init_all_table(){
  var cpp_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var cppp_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停',
    '标记'
  ]

  var curry_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var lv_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '周期',
    '近15天涨停'
  ]

  var durant_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var yao_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var rondo_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  var rondop_header=[
    '股票编号',
    '近期中签',
    '预测时间',
    '近15天涨停'
  ]

  init_table('#cpp-table', cpp_header);
  init_table('#cppp-table', cppp_header);
  init_table('#curry-table', curry_header);
  init_table('#lv-table', lv_header);
  init_table('#durant-table', durant_header);
  init_table('#yao-table', yao_header);
  init_table('#rondo-table', rondo_header);
  init_table('#rondop-table', rondop_header);
}

function init_table(id, header){
  var html_res = '<tr>'
  for(var i in header){
    html_res+='<td>'+header[i]+'</td>';
  }
  html_res+='</tr>';
  $(id).html(html_res);
}

function load_data(id, data){
  var str = "";
  for(var i in data){
    child = data[i];
    str += '<tr>';
    for(var j in child){
      if(j==0){
        str+='<td = class="td1">'+child[j]+'</td>';
      }
      else{
        str+='<td>'+child[j]+'</td>';
      }
    }
    str+='</tr>';
  }
  $(id).append(str);
}


function updateTable(data){
  var lines = data.split('\n');
  var info_str = '';
  var cpp_data = [];
  var cppp_data = [];
  var lavine_data = [];
  var curry_data = [];
  var durant_data = [];
  var yao_data = [];
  var rondo_data = [];
  var rondop_data = [];
  for(var i=0;i<lines.length;i++){
    items = lines[i].split(',');
    /*last90 = items.pop();*/
    switch(items[0]){
      case 'num':
        $('#pre-num').text('请求数目：'+items[1]);
        break;
      case 'endtime':
        $('#pre-time').text('刷新时间：'+items[1]);
        break;
      case 'time':
        $('#pre-used').text('预测耗时'+items[1]);
        break;
      case 'cpp':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        cpp_data.push(element);
        break;
      case 'cppp':
        var flag = '-';
        if(items[5]==1) flag = 'A';
        if(items[5]==2) flag = 'B';
        var element = [
          items[1],
          items[3],
          items[2],
          items[4],
          flag
        ];
        cppp_data.push(element);
        break;
      case 'curry':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        curry_data.push(element);
        break;
      case 'lavine':
        var element = [
          items[1],
          items[4],
          items[2],
          items[3],
          items[5]
        ];
        lavine_data.push(element);
        break;
      case 'durant':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        durant_data.push(element);
        break;
      case 'yao':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        yao_data.push(element);
        break;
      case 'rondo':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        rondo_data.push(element);
        break;
      case 'rondop':
        var element = [
          items[1],
          items[3],
          items[2],
          items[4]
        ];
        rondop_data.push(element);
        break;
      default:
        break;
    }
  }
  load_data('#cpp-table', cpp_data);
  load_data('#cppp-table', cppp_data);
  load_data('#curry-table', curry_data);
  load_data('#lv-table', lavine_data);
  load_data('#durant-table', durant_data);
  load_data('#yao-table', yao_data);
  load_data('#rondo-table', rondo_data);
  load_data('#rondop-table', rondop_data);
}
